# JUDICIAL PROFESSIONAL BACKGROUND AND PRETRIAL DETENTION OUTCOMES
# Oded Oren and Chad Topaz
# Outputs: Table 10 (VFO subset)
library("this.path")
source(paste0(dirname(this.path()), "/functions.R"))


# Load Data ---------------------------------------------------------------
judgeinfo <- readRDS(paste0(dirname(this.path()), "/data/judgeinfo.RDS")) %>%
  select(-detailed_experience)

detentiondata <- readRDS(paste0(dirname(this.path()), "/data/detentiondata.RDS")) %>%
  filter(topseverity == "VFO") %>%
  select(-topseverity) %>%
  droplevels()

baildata <- readRDS(paste0(dirname(this.path()), "/data/baildata.RDS")) %>%
  filter(topseverity == "VFO") %>%
  select(-topseverity) %>%
  droplevels()


# Models ------------------------------------------------------------------
## Main -----
### Detention Model — Table 10 -------------------------------------------------
vars_detention <- setdiff(names(detentiondata), c("detained", "judge", "law_enforcement", "legal_services"))

model_detention <- feols(
  as.formula(paste("detained ~", paste(vars_detention, collapse = " + "))),
  data = detentiondata,
  cluster = ~ judge)
summary(model_detention)

### Bail Extensive Model — Table 10 -------------------------------------------
data_extensive <- baildata %>%
  mutate(bail_set = cash.bail > 0)

vars_extensive <- setdiff(
  names(data_extensive),
  c("bail_set",
    "cash.bail",
    "judge",
    "law_enforcement",
    "legal_services")
)

model_extensive <- feols(
  as.formula(paste("bail_set ~", paste(vars_extensive, collapse = " + "))),
  data    = data_extensive,
  cluster = ~ judge
)

summary(model_extensive)

### Bail Intensive Model — Table 10 -------------------------------------------
data_intensive = baildata %>%
  filter(cash.bail > 0) %>%
  mutate(log_bail = log(cash.bail))

model_intensive <- lm(
  log_bail ~ . - judge - cash.bail - law_enforcement - legal_services - log_bail,
  data = data_intensive)

vcov_intensive  <- vcovCL(model_intensive,
                          data = data_intensive,
                          cluster = ~judge,
                          type = "HC1")

print(coeftest(model_intensive, vcov = vcov_intensive))

## Law Enforcement vs. All Others — Table 10 (LE panel)----
### Detention Model — Table 10 (LE vs All) ------------------------------------
vars_detention_LE <- setdiff(
  names(detentiondata),
  c("detained", "judge", "experience",
    "legal_services")
)

model_detention_LE <- feols(
  as.formula(paste("detained ~", paste(vars_detention_LE, collapse = " + "))),
  data    = detentiondata,
  cluster = ~ judge
)
summary(model_detention_LE)

### Bail Extensive Model — Table 10 (LE vs All) -------------------------------
data_extensive_LE <- baildata %>%
  mutate(bail_set = cash.bail > 0)

vars_extensive_LE <- setdiff(
  names(data_extensive_LE),
  c("bail_set",
    "cash.bail",
    "judge",
    "experience",
    "legal_services")
)

model_extensive_LE <- feols(
  as.formula(paste("bail_set ~", paste(vars_extensive_LE, collapse = " + "))),
  data    = data_extensive_LE,
  cluster = ~ judge
)

summary(model_extensive_LE)

### Bail Intensive Model — Table 10 (LE vs All) -------------------------------
data_intensive_LE <- baildata %>%
  filter(cash.bail > 0) %>%
  mutate(log_bail = log(cash.bail))

model_intensive_LE <- lm(
  log_bail ~ . -judge -cash.bail -experience -legal_services -log_bail,
  data = data_intensive_LE
)

vcov_intensive_LE <- vcovCL(model_intensive_LE,
                            data = data_intensive_LE,
                            cluster = ~judge,
                            type    = "HC1")

print(coeftest(model_intensive_LE, vcov = vcov_intensive_LE))

## Legal Services vs. All Others  — Table 10 (LS panel)-----
### Detention Model — Table 10 (LS vs All) ------------------------------------
vars_detention_LS <- setdiff(
  names(detentiondata),
  c("detained", "judge", "experience", "law_enforcement")
)

model_detention_LS <- feols(
  as.formula(paste("detained ~", paste(vars_detention_LS, collapse = " + "))),
  data    = detentiondata,
  cluster = ~ judge
)
cat("\n=== Detention model (legal_services) ===\n")
summary(model_detention_LS)


### Bail Extensive Model — Table 10 (LS vs All) -------------------------------
data_extensive_LS <- baildata %>%
  mutate(bail_set = cash.bail > 0)

vars_extensive_LS <- setdiff(
  names(data_extensive_LS),
  c("bail_set",
    "cash.bail",
    "judge",
    "experience",
    "law_enforcement")
)

model_extensive_LS <- feols(
  as.formula(paste("bail_set ~", paste(vars_extensive_LS, collapse = " + "))),
  data    = data_extensive_LS,
  cluster = ~ judge
)

summary(model_extensive_LS)

### Bail Intensive Model — Table 10 (LS vs All) -------------------------------
data_intensive_LS <- baildata %>%
  filter(cash.bail > 0) %>%
  mutate(log_bail = log(cash.bail))

model_intensive_LS <- lm(
  log_bail ~ . -judge -cash.bail -experience -law_enforcement -log_bail,
  data = data_intensive_LS
)

vcov_intensive_LS <- vcovCL(model_intensive_LS,
                            data = data_intensive_LS,
                            cluster = ~judge,
                            type    = "HC1")

print(coeftest(model_intensive_LS, vcov = vcov_intensive_LS))

# Generate Table 10----

table_10(
  model_detention,  model_extensive,  model_intensive,  vcov_intensive,
  model_detention_LE, model_extensive_LE, model_intensive_LE, vcov_intensive_LE,
  model_detention_LS, model_extensive_LS, model_intensive_LS, vcov_intensive_LS
)
